package com.didi.gulimall.mq;

import com.alibaba.fastjson.JSON;
import com.didi.gulimall.aop.MqLog;
import com.didi.gulimall.common.entity.LocalMqEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author 10626
 * @Create 2021/6/3 20:13
 */
@Component
public class GuliRabbitTemplate {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @MqLog
    public void sendConvertAndSend(LocalMqEntity localMqEntity) {
        String body = localMqEntity.getBody();
        try {
            Class<?> c = Class.forName(localMqEntity.getClassType());
            Object o = JSON.parseObject(body, c);
            rabbitTemplate.convertAndSend(localMqEntity.getExchange(), localMqEntity.getRouteKey(), o, new CorrelationData(localMqEntity.getId()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
